<?php defined('SYSPATH') or die('No direct script access.');
/**
* Forum Function Helper
*/
class Helper_productFunctions {
	
	static function &factory() {
	   $o = new Helper_productFunctions();
	  return $o;	
	}
	
  // Find Total Child Data
	public function find_group_child($parent_table, $child_table,$parent_id)
	{
		$count_total_child = DB::select('id')
			->from($child_table)
			->where($parent_table.'_id','=',$parent_id)
			->execute();
		$total_child = $count_total_child->count();
		
		return $total_child;
	}
	
  // Find Product Images from ID
	public function product_images($ids=NULL,$value=NULL)
	{
	  // Find Option Value
		$productimages = DB::select()
			->from('prodimages');
		
		switch($value)
		{
		case "products":
				  // Find From Pruducts ID Field
					$productimages = $productimages->where('products_id','=',$ids);
					break;
		
		case "skus":
				  // Find From Pruducts SKU ID Field
					$productimages = $productimages->where('productskus_id','=',$ids);
					break;
		}
			
		$productimages = $productimages
			->order_by('datetime','DESC')
			->execute();
		
		$imagearrays = $productimages->as_array();
		
		return $imagearrays;
	}

	
  // Create List of Array From Table Product Options
  // Give $empty_strings = NULL if don't want to set the Empty Strings
	public function product_option_lists($parent_id, $empty_strings='Choose Selection')
	{
		$product_option_lists = DB::select('id','value')
			->from('prodoptions')
			->where('prodoptiongroups_id', '=', $parent_id)
			->where('approve', '!=', NULL)
			->order_by('position', 'ASC')
			->execute();
		$product_option_arrays = $product_option_lists->as_array();
		
		if(!is_null($empty_strings)) {
			$arraylist[0] = $empty_strings;
		}
		
		foreach($product_option_arrays as $product_option_array)
		{
			$arraylist[$product_option_array['id']] = $product_option_array['value'];
		}
		
		return $arraylist;
	}
	
	public function show_product($prodcategories=NULL,$prodsubcategories=NULL,$prodbrands=NULL,$order=NULL,$limit=NULL,$newitem=NULL)
	{
		
		$product = DB::select()
			->from('products');
				if(!empty($prodcategories)) {
					$product = $product->where('prodcategories_id','=',$prodcategories);
				}
				if(!empty($prodsubcategories)) {
					$product = $product->where('prodsubcategories_id','=',$prodsubcategories);
				}
				if(!empty($prodbrands)) {
					$product = $product->where('prodbrands_id','=',$prodbrands);
				}
			if(!empty($newitem)) {
				$product = $product->where('newitem','=',$newitem);
			}
			if(!empty($order)) {
			$product = $product->order_by('position',$order);
			}
			if(!empty($limit)) {
			$product = $product->limit($limit);
			}
			$product = $product->execute();
		$products = $product->as_array();
		return $products;
	}
	public function array_skus_by_prod_id($products_id,$order=NULL,$limit=NULL)
	{
		
		$product = DB::select()
			->from('prodskus');
				if(!empty($products_id)) {
					$product = $product->where('products_id','=',$products_id);
				}
				if(!empty($order)) {
				$product = $product->order_by('value',$order);
				}
				if(!empty($limit)) {
				$product = $product->limit($limit);
				}
			$product = $product->execute();
		$products = $product->as_array();
		return $products;
	}
	public function products_array($status=NULL)
	{
		$ss			= Session::instance();
		$logid	=	$ss->get('logid');
		
		if(!empty($logid)) {
			$dataarray = DB::select()
			->from('carts');
			//if(!is_null($status)){
				$dataarray->where('status','=',$status);
			//}
			$defaultarrays = $dataarray->execute()->as_array();
			//$defaultarrays(
				//array(
					//'name'=>$dataarrays[0]
						 //));
		}else{
			$defaultarrays = $ss->get('product');
		}
		
		return $defaultarrays;
	}

	public function count_products($table)
	{
		$query = DB::select()
							->from($table)
							->where('status','=',NULL)
							->execute();
			$count = $query->count();
		
		return $count;
	}

	public function array_products($empty_strings='Choose Selection')
	{
		$thesystems = Helper_thesystems::factory();
		$type       = $thesystems->system_auth('module_products_detail_type');
		if($type == 'skus') 
		{
			$table = 'skus';
			$value = 'value';
		} else {
			$table = 'ucts';
			$value = 'name';
		}

		$dataarray = DB::select('id',$value,'stock')
			->from('prod'.$table)
			->where('approve', '!=', NULL);
			//->where('stock','>',0)
			if($type != 'skus') 
			{
			$dataarray = $dataarray->order_by('position', 'ASC');
			}
			$dataarray = $dataarray->execute();
		$defaultarrays = $dataarray->as_array();
		
		if(!is_null($empty_strings)) {
			$arraylist[0] = $empty_strings;
		}
		
		foreach($defaultarrays as $defaultarray)
		{
			$arraylist[$defaultarray['id']] = $defaultarray[$value].' ('.$defaultarray['stock'].')';
		}
		
		return $arraylist;
	}

	public function count_product_stocks($products_id)
	{
		$query = DB::select('stock')
			->from('products')
			->where('id','=',$products_id)
			->where('approve','!=',NULL)
			->execute()
			->as_array();
			
		if(!empty($query)) 
		{
			return $query[0]['stock'];
		} else {
			return '0';
		}

	}
	
	public function prod_terakhir_dibeli($invoice_id,$views='harga')
	{
		$display          = Helper_DisplayItems::factory();
		$query = DB::select()
			->from('acctransactions')
			->where('accinvoices_id','=',$invoice_id)
			->order_by('id','DESC')
			->limit('1')
			->execute()
			->as_array();

		if(!empty($query)) 
		{
			if($views == 'harga') 
			{
				$harga = $display->show_value($query[0]['items'],'products','price01');
				$totalitems = $query[0]['totalitems'];
				return $harga*$totalitems;
			} else {
				$nama = $display->show_value($query[0]['items'],'products','name');
				return $nama;
			}
		} else {
			return '0';
		}

	}
	
	public function total_harga($invoice_id)
	{
		$officefunctions = Helper_officefunctions::factory();

		$harga = 0;
		$query = DB::select()
			->from('acctransactions')
			->where('accinvoices_id','=',$invoice_id)
			->where('acccodes_keys','=','21102')
			->execute()
			->as_array();

		if(!empty($query)) 
		{
			foreach($query as $prod_id) 
			{
				$harga += $officefunctions->check_harga($prod_id['items'],$prod_id['totalitems']);
			}
			return $harga;
		} else {
			return '0';
		}

	}
	
	public function product_stock($product_id)
	{
		$stok_manual=$this->product_manual_stock($product_id);
		$stok=0;
		$query = DB::select(
			array('accinvoices.id','invoices_id'))
			->from('accinvoices')
				->join('acctransactions')
				->on('acctransactions.accinvoices_id','=','accinvoices.id')
			->where('accinvoices.tambah_stok','=',1)
			->where('acctransactions.items','=',$product_id)
			->execute()
			->as_array();

		if(!empty($query)) 
		{
			foreach($query as $transaction) {
				$query2 = DB::select(
					array('acctransactions.totalitems','stok'))
					->from('accinvoices')
						->join('acctransactions')
						->on('acctransactions.accinvoices_id','=','accinvoices.id')
					->where('acctransactions.items','=',$product_id)
					->execute()
					->as_array();
				$stok += $query2[0]['stok'];
			}
			return $stok+$stok_manual;
		} else {
			return $stok_manual;
		}

	}
	
	public function product_manual_stock($product_id)
	{
		$stok=0;
		$query = DB::select()
			->from('productstock')
			->where('product_id','=',$product_id)
			->execute()
			->as_array();

		if(!empty($query)) 
		{
			foreach($query as $transaction) {
				$query2 = DB::select()
					->from('productstock')
					->where('product_id','=',$product_id)
					->execute()
					->as_array();
				$stok += $query2[0]['totalitems'];
			}
			return $stok;
		} else {
			return '0';
		}

	}

	public function check_pecahan($product_id,$totalitems)
	{
		$query = DB::select()
			->from('products')
			->where('id','=',$product_id)
			->execute()
			->as_array();

		$pecahan = $query[0]['pecahan'];
		if(!empty($pecahan)) 
		{
			return number_format($totalitems,2);
		} else {
			return number_format($totalitems,0);
		}

	}

	public function check_pecahan_only($product_id)
	{
		$query = DB::select()
			->from('products')
			->where('id','=',$product_id)
			->execute()
			->as_array();

		$pecahan = $query[0]['pecahan'];
		if(!empty($pecahan)) 
		{
			return '1';
		} else {
			return '0';
		}

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}